import store from "@/store/index.js";
// api.js
// 封装网络请求方法
function request(url, method = 'GET', data = {} , ContentType) {
	
	// const domain='https://api.icode2023.top';
	const domain='https://api.ztougj.cc';
	let userData = uni.getStorageSync('userData');
	// 有请求的页面设置颜色
	uni.setNavigationBarColor({
		frontColor: '#ffffff',
	    backgroundColor: store.state.presentColor
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: domain+url,
			method: method,
			data: data,
			header : {
				"token" : userData.token ? userData.token : undefined,
				"Content-Type": ContentType
			},
			success: (res) => {
				const statusCode = res.statusCode
				const response = res.data;
				// 判断请求状态码
				if (statusCode === 200) {
					if(response.code == '1'){
						resolve(response)
					}else{
						uni.showModal({
							title:'提示',
							content:res.data.msg || res.data.message,
							showCancel:false,
							success: () => {
								resolve(response)
							}
							
						})
					}
				} else {
					reject(response)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

// 封装GET请求方法
function get(url, data = {} , ContentType =  'application/json;charset=UTF-8') {
	return request(url, 'GET', data , ContentType)
}

// 封装POST请求方法
function post(url, data = {} , ContentType =  'application/json;charset=UTF-8') {
	return request(url, 'POST', data , ContentType)
}

export default {
	get,
	post
}